#include "StdAfx.h"
#include "Car.h"
#include "CarManager.h"

CCar::CCar(void)
{
	_position = CGrVector(0, 0, 0);
	_lastPCounter = 0;
	addPosition(_position);

	_clength = 1;
	_cwidth = 0.4;
	_cheight = 0.2;
	_floorLift = 0.000001;
	_wheelR = 0.1;

	//people sizes
	_peopleR = 0.075;
	_peoplePadX = 0.02;
	_peoplePadZ = 0.01;

	_numHostages = 2; //default now to 2 hostages
	_velocity = 0.1; //it will go .1 of the curve every second
	_bcurve.setControlPoint(0, CGrVector(0, 0, 0));
	_bcurve.setControlPoint(1, CGrVector(0, 0, 5));
	_bcurve.setControlPoint(2, CGrVector(20, 0, -5));
	_bcurve.setControlPoint(3, CGrVector(20, 0, 20));
	_bcurve.calculateCoefficients();
	_bezierTravel = 0;

	_manager = 0;
}

CCar::~CCar(void)
{
}

void CCar::Update(double dt)
{
	_bezierTravel += dt * _velocity;

	_position.X(_bcurve.X(_bezierTravel));
	_position.Y(_bcurve.Y(_bezierTravel));
	_position.Z(_bcurve.Z(_bezierTravel));

	//calculate rotation
	if (_position != _lastPositions[0])
	{
		CGrVector dir = _position - _lastPositions[0];
		dir.Normalize3();
		_rotation = atan2(-dir.Z() , dir.X());
	}

	addPosition(_position);

	if (_bezierTravel > 1 && _manager != 0)
	{
		_manager->removeCar(_ID);
	}
}

void CCar::Render()
{
	glPushMatrix();
	glTranslated(_position.X(), _position.Y(), _position.Z());
	glRotated(_rotation * 180. / M_PI, 0, 1, 0);

	glColor3f(0.8f, 0.2f, 0.f);

	DrawBody();
	DrawWheels();
	DrawPeople();
	glTranslated(-_position.X(), -_position.Y(), -_position.Z());

	glPopMatrix();

	//_bcurve.debug_DrawCurve();
}

void CCar::DrawBody()
{
	glBegin(GL_QUADS);

	//length is the x-axis, width is the z-axis
	//wheelR is the wheel radius, consequently the 'height' of the car
	//built in this order -- bottom, front, back. -z side, z side
	//

	//bottom piece
	glColor3f(0.2f, 0.2f, 0.f);
	glNormal3d(0, -1, 0);
	glVertex3d(-_clength / 2.0, _wheelR, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR, _cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR, _cwidth / 2.0);

	//front of car (with headlights)
	glColor3f(0.8f, 0.2f, 0.f);
	glNormal3d(1, 0, 0);
	glVertex3d(_clength / 2.0, _wheelR, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR, _cwidth / 2.0);

	//back of car (with tail-lights)
	glNormal3d(-1, 0, 0);
	glVertex3d(-_clength / 2.0, _wheelR, _cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR, -_cwidth / 2.0);

	//-z-axis side panel
	glNormal3d(0, 0, -1);
	glVertex3d(-_clength / 2.0, _wheelR, -_cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR, -_cwidth / 2.0);
	//z-axis side panel
	glNormal3d(0, 0, 1);
	glVertex3d(-_clength / 2.0, _wheelR, _cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR, _cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);

	//front lip (dashboard?)
	glColor3f(0.f, 0.4f, 0.7f);
	glNormal3d(0, 1, 0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);
	//back lip
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(-_clength / 2.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.0);
	//side lip (-z axis side)
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.0);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.0);
	//side lip (+z axis side)
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, _cwidth / 2.0);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);

	//inside door panel (-z axis side)
	glColor3f(0.1f, 0.7f, 0.342f);
	glNormal3d(0, 0, 1);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	//inside door panel (+z axis side)
	glNormal3d(0, 0, -1);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR, _cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR, _cwidth / 2.5);

	//back inside panel (-x axis side)
	glColor3f(0.287f, 0.4872f, 0.62938f);
	glNormal3d(1, 0, 0);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR, _cwidth / 2.5);
	glVertex3d(-_clength / 3.0, _wheelR, -_cwidth / 2.5);
	//front inside panel (+x axis side)
	glNormal3d(-1, 0, 0);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, _cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR + _cheight, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR, -_cwidth / 2.5);
	glVertex3d(_clength / 3.0, _wheelR, _cwidth / 2.5);

	//inside floor piece
	double hh = _wheelR + _floorLift;
	glColor3f(0.23f, 0.1248f, 0.429f);
	glNormal3d(0, 1, 0);
	glVertex3d(-_clength / 3.0, hh, -_cwidth / 2.5);
	glVertex3d(-_clength / 3.0, hh, _cwidth / 2.5);
	glVertex3d(_clength / 3.0, hh, _cwidth / 2.5);
	glVertex3d(_clength / 3.0, hh, -_cwidth / 2.5);

	glEnd();
}

void CCar::DrawPeople()
{
	glPushMatrix();

	//set up translation call
	glColor3f(0.56349f, 0.1f, 0.1245f);
	glTranslated(-_clength / 3.0 + _peopleR, _cheight + _peopleR*2., -_cwidth / 2.5 + _peopleR);

	for (int i = 0; i < _numHostages; i++)
	{
		if (i % 2 == 0) //driver's side (left)
		{
			glPushMatrix();
			glTranslated( i * (_peopleR + _peoplePadX), 0, 0);
			drawSphere(_peopleR, 16, 16);
			glPopMatrix();
		}
		else //passenger's side (right)
		{
			glPushMatrix();
			glTranslated( (i-1) * (_peopleR + _peoplePadX), 0, 2.*(_peopleR + _peoplePadZ));
			drawSphere(_peopleR, 16, 16);
			glPopMatrix();
		}
	}
	glPopMatrix();
}

void CCar::DrawWheels()
{
	glPushMatrix();
	glTranslated(-_clength / 2.0 + _wheelR, _wheelR, -_cwidth / 2.0 + _wheelR);

	//rear-left wheel
	glColor3f(0.3f, 0.3f, 0.3f);
	drawSphere(_wheelR, 16, 16);

	glTranslated(_clength - 2.*_wheelR, 0, 0);
	drawSphere(_wheelR, 16, 16);

	glTranslated(0, 0, _cwidth - 2.*_wheelR);
	drawSphere(_wheelR, 16, 16);
	
	glTranslated(-_clength + 2.*_wheelR, 0, 0);
	drawSphere(_wheelR, 16, 16);

	glPopMatrix();
}

void CCar::addPosition(CGrVector lp)
{
	for (int i = _lastPCounter-1; i > 0; i--)
		_lastPositions[i-1] = _lastPositions[i];

	if (_lastPCounter < 10) _lastPCounter++;

	_lastPositions[_lastPCounter-1] = lp;
}